<?php
class ControllerArticleTerm extends ListController {

    protected function heading_title() {
        if(isset($this->request->get['taxonomy'])){
            $taxonomy = $this->request->get['taxonomy'];
            if($taxonomy == "category"){
                return "标签列表";
            }
        }
        return "分类列表";
    }
    protected function primary_key_name() {
        return "term_id";
    }
    protected function class_path() {
        return "article/term";
    }
    protected function model_name() {
        return "model_article_term";
    }
    protected function extra_params(){
        if(isset($this->request->get['taxonomy'])){
            return "taxonomy={$this->request->get['taxonomy']}";
        }
        return "";
    }


    protected function get_list_data(&$ret,$filter_data,$url)
    {
        $total = $this->{$this->model_name()}->getCount();

        $filter_data['taxonomy'] = 'category';
        $ret = $this->{$this->model_name()}->getList($filter_data);



        return $total;
    }


    protected function get_form_data(&$data){

        $item = null;
        if (isset($this->request->get[$this->primary_key_name()]) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $item = $this->{$this->model_name()}->getItem($this->request->get[$this->primary_key_name()]);
        }

        $this->load->model('tool/image');

        if (isset($this->request->post['name'])) {
            $data['name'] = $this->request->post['name'];
        } elseif (isset($item)) {
            $data['name'] = $item['name'];
        } else {
            $data['name'] = '';
        }

        if (isset($this->request->post['image'])) {
            $data['image'] = $this->request->post['image'];
        } elseif (isset($item)) {
            $data['image'] = $item['image'];
        } else {
            $data['image'] = '';
        }

        $data['placeholder'] = $this->model_tool_image->resize('no_image.png', 100, 100);

        if (is_file(DIR_IMAGE . $data['image'])) {
            $data['thumb'] = $this->model_tool_image->resize($data['image'], 100, 100);
        }
        else{
            $data['thumb'] = $data['placeholder'];
        }


    }


    protected function validateForm() {
        if (!$this->user->hasPermission('modify', 'catalog/information')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        foreach ($this->request->post['information_description'] as $language_id => $value) {
            if ((utf8_strlen($value['title']) < 3) || (utf8_strlen($value['title']) > 64)) {
                $this->error['title'][$language_id] = $this->language->get('error_title');
            }

            if (utf8_strlen($value['description']) < 3) {
                $this->error['description'][$language_id] = $this->language->get('error_description');
            }

            if ((utf8_strlen($value['meta_title']) < 3) || (utf8_strlen($value['meta_title']) > 255)) {
                $this->error['meta_title'][$language_id] = $this->language->get('error_meta_title');
            }
        }

        if (utf8_strlen($this->request->post['keyword']) > 0) {
            $this->load->model('catalog/url_alias');

            $url_alias_info = $this->model_catalog_url_alias->getUrlAlias($this->request->post['keyword']);

            if ($url_alias_info && isset($this->request->get['information_id']) && $url_alias_info['query'] != 'information_id=' . $this->request->get['information_id']) {
                $this->error['keyword'] = sprintf($this->language->get('error_keyword'));
            }

            if ($url_alias_info && !isset($this->request->get['information_id'])) {
                $this->error['keyword'] = sprintf($this->language->get('error_keyword'));
            }
        }

        if ($this->error && !isset($this->error['warning'])) {
            $this->error['warning'] = $this->language->get('error_warning');
        }

        return !$this->error;
    }

    protected function validateDelete() {
        if (!$this->user->hasPermission('modify',$this->class_path())) {
            $this->error['warning'] = $this->language->get('error_permission');
            return !$this->error;
        }

        $this->load->model($this->class_path());

        if (isset($this->request->post['selected']) ) {
            foreach ($this->request->post['selected'] as $id) {
                if(!$this->{$this->model_name()}->can_delete($id)){
                    $this->error['warning'] = "无法删除有关联的数据";
                    return !$this->error;
                }


            }
        }

        return !$this->error;
    }
}